package affichage;

import method.Tablier;
import java.util.List;
import java.awt.*;
import java.util.ArrayList;
import java.util.Set;
import javax.swing.*;
import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;

public class TablierGraph extends JFrame {

    private int size;
    private Tablier main;
    private int n;
    private String ret;
    private Set<Integer> ens;
    private int max;

    public TablierGraph(Tablier main) {
        super("Test : colorF2");
        this.main = main;
        this.max = main.getMax();
        this.n = main.getN();
        this.ens = main.getEns();
        size = n;
        initialize(size);
        affichage();

    }

    public void affichage() {
        ret = "";
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                ret += main.getMap().get(main.getTab()[i][j]) + ",";
            }
            ret += '\n';
        }
        System.out.println(ret);
        System.out.println(ens.size());
    }

    public TablierGraph(int boardSize) {
        super("Test : colorF2");
        this.size = boardSize;
        initialize(boardSize);

    }

    private void initialize(int boardSize) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Create the GUI
        JPanel topLevel = new JPanel();
        topLevel.setLayout(new BorderLayout());
        getContentPane().add(topLevel);
        // Create the graphic canvas
        GWindow window = new GWindow(new Color(220, 220, 220));
        topLevel.add(window.getCanvas(), BorderLayout.CENTER);
        // Create scene
        GScene scene = new GScene(window);
        double[] w0 = {0.0, 0.0, 0.0};
        double[] w1 = {boardSize + 2.0, 0.0, 0.0};
        double[] w2 = {0.0, boardSize + 2.0, 0.0};
        scene.setWorldExtent(w0, w1, w2);
        // Create  graphics representation
        GObject board = new Board();
        scene.add(board);
        pack();
        setSize(new Dimension(1000, 1000));
        setVisible(true);
    }

    class Board extends GObject {

        private static final int ech = 30;
        private static final int cote = 30;
        private List<GSegment> states;
        private GStyle[] stateStyle;
        private final GStyle textStyle;
        private GText[][] gtext;

        public Board() {
            stateStyle = new GStyle[ max + 1 ];
            for (int s = 0 ; s < max + 1 ; s++) {
                stateStyle[s] = new GStyle();
                Color color = colorF3(s);
                stateStyle[s].setForegroundColor(color);
                stateStyle[s].setBackgroundColor(color);
            }
            states = new ArrayList<GSegment>();

            textStyle = new GStyle();
            textStyle.setForegroundColor(new Color(0, 0, 0));
            textStyle.setFont(new Font("Dialog", Font.BOLD, 10));

            gtext = new GText[ n + 1 ][ n + 1 ];
            for (int j = 1 ; j <= n ; j++) {
                for (int i = 1 ; i <= n ; i++) {
                    gtext[i][j] = new GText(String.valueOf(main.getMap().get(main.getTab()[i][j])));
                    gtext[i][j].setStyle(textStyle);
                    gtext[i][j].setPositionHint(GPosition.SOUTH);
                }
            }
        }

        private Color colorF1(int s) {
            return new Color(0, 0, (200 * s) / 7 + 55);
        }

        private Color colorF3(int s) {
            int val = 255 * s / max;
            return new Color(val, val, val);
        }

        private Color colorF2(int s) {

            switch (s) {
                case 0:
                    return Color.GRAY;
                case 1:
                    return Color.CYAN;
                case 2:
                    return Color.LIGHT_GRAY;
                case 3:
                    return Color.YELLOW;
                case 4:
                    return Color.ORANGE;
                case 5:
                    return Color.RED;
                case 6:
                    return Color.GREEN;
                case 7:
                    return Color.MAGENTA;
                default:
                    new Exception("illegal argument");
                    return null;
            }
        }

        @Override
        public void draw() {
            for (int j = 1 ; j <= n ; j++) {
                for (int i = 1 ; i <= n ; i++) {
                    GSegment etat = new GSegment();
                    states.add(etat);
                    addSegment(etat);
                    etat.setStyle(stateStyle[main.getMap().get(main.getTab()[i][j])]);
                    etat.setGeometry(Geometry.createRectangle(i * ech, j * ech, cote, cote));
                    etat.addText(gtext[i][j]);
                }
            }

        }
    }
}
